﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleUI
{
    class Program
    {
        static void Main(string[] args)
        {
            var tempInt = 0;
            var cards = CardHelper.IniCard(2, true);
            //场上的牌
            var currentCards = new List<Card>();
            //几连手牌
            var recCount = 8;
            var sends = CardHelper.SendCard(cards, 2);
            var currentCardsA = (List<Card>)sends[0];
            var currentCardsB = (List<Card>)sends[1];


            Console.BackgroundColor = ConsoleColor.Green;
            Console.ForegroundColor = ConsoleColor.White;
            while (true)
            {
                if (ShowWinner(currentCardsA, currentCardsB))
                {
                    tempInt = 99;
                }

                if (tempInt == 0)
                {
                    Console.WriteLine("请玩家A扔牌");
                    Console.ReadLine();
                    var loss = currentCardsA[0];
                    tempInt = 1;
                    Console.WriteLine($"玩家A扔出：{loss},手牌:{currentCardsA.Count - 1}");
                    currentCards.Add(currentCardsA[0]);//场上牌加一
                    currentCardsA.RemoveAt(0);//玩家A -1 
                    //TODO CHECK
                    var checkCards = CheckCards(currentCards, loss, recCount);
                    if (checkCards.Count() > 0)
                    {
                        currentCardsA.AddRange(checkCards);
                        Utils.RandomSort(currentCardsA);
                        checkCards.ForEach(x => { currentCards.Remove(x); });
                        Console.WriteLine($"玩家A走了狗屎运,扔出的牌{recCount}连,手牌:{currentCardsA.Count}");
                    }
                }
                else if (tempInt == 1)
                {
                    Console.WriteLine("请玩家B扔牌");
                    Console.ReadLine();

                    tempInt = 0;
                    var loss = currentCardsB[0];

                    Console.WriteLine($"玩家B扔出：{loss},手牌:{currentCardsB.Count - 1}");
                    currentCards.Add(currentCardsB[0]);//场上牌加一
                    currentCardsB.RemoveAt(0);//玩家A -1 

                    var checkCards = CheckCards(currentCards, loss, recCount);
                    if (checkCards.Count() > 0)
                    {
                        currentCardsB.AddRange(checkCards);
                        Utils.RandomSort(currentCardsB);
                        checkCards.ForEach(x => { currentCards.Remove(x); });
                        Console.WriteLine($"玩家B吃了一泡狗屎。。。,扔出的牌{recCount}连,手牌:{currentCardsB.Count}");
                    }
                }
                else if (tempInt == 99)
                {
                    Console.WriteLine("胜负一份");
                    Console.ReadLine();
                }
                if (tempInt != 99)
                    ShowCards(currentCards);
            }
        }
        static void ShowCards(List<Card> cards)
        {
            Console.WriteLine("场上牌------------------");
            cards.ForEach(x =>
            {
                switch (x.Color)
                {
                    case 0:
                    case 1:
                        Console.ForegroundColor = ConsoleColor.Red;
                        break;
                    case 2:
                    case 3:
                        Console.ForegroundColor = ConsoleColor.Black;
                        break;
                    default:
                        break;
                }
                Console.WriteLine(x.ToString());
                Console.ForegroundColor = ConsoleColor.White;
            });
            Console.WriteLine("--------------------");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="cards">场上牌</param>
        /// <param name="count">几连可以收牌</param>
        /// <returns></returns>
        static List<Card> CheckCards(List<Card> cards, Card lastCard, int count)
        {
            var retval = new List<Card>();
            if (cards.Count(x => x.Name == lastCard.Name) == count)
            {
                var c = cards.IndexOf(cards.First(x => x.Name == lastCard.Name));
                for (int i = c; i < cards.Count; i++)
                {
                    retval.Add(cards[i]);
                }
            }
            return retval;
        }

        static bool ShowWinner(List<Card> currentCardsA, List<Card> currentCardsB)
        {
            if (currentCardsA.Count == 0)
            {
                Console.WriteLine("玩家B胜利！！！！！");
                Console.WriteLine("玩家B胜利！！！！！");
                Console.WriteLine("玩家B胜利！！！！！");
                return true;
            }
            if (currentCardsB.Count == 0)
            {
                Console.WriteLine("玩家A胜利！！！！！");
                return true;
            }
            return false;
        }
    }
}
